package descentFly.away3D4.element.Extends
{
	import UIFree.ObjectEvent;
	
	import UIUtils.uif_util;
	
	import away3d.tools.utils.Bounds;
	
	import descentFly.app.df_events;
	import descentFly.away3D4.chips.df_chipAway;
	import descentFly.away3D4.element.df_element;
	import descentFly.chips.df_chip;
	import descentFly.df_space;
	import descentFly.df_spaceObject;
	import descentFly.sound.df_soundManager;
	
	import flash.events.Event;
	import flash.geom.Vector3D;
	
	public class df_distanceController extends df_controller {
		private var _nearest	: Number;
		private var _nearState	: Boolean;	
		private var _myChip		: df_chipAway;
		public function df_distanceController(a_ctrl:df_element, a_options:Object) {
			super(a_ctrl, a_options);
			_nearState = false;
			ctrl.addEventListener(df_element.SETMODEL, doSetModel);
		}
		
		protected function doSetModel(e: Event): void {
			ctrl.removeEventListener(df_element.SETMODEL, doSetModel);
			ctrl.Space.callWaitEvent2(ctrl.Space.myChip != null, df_space.SETMYCHIP, resetMyChip);
		}
		
		private function resetMyChip(): void {
			if (_myChip != ctrl.Space.myChipAway) {
				if (_myChip == null)
					playSound('init', ctrl.getModel()); // Проигрываем звук только один раз, при возникновении артефакта
				
				_myChip = ctrl.Space.myChipAway;
				listenMyChipEvents();
				_nearest = getNearest();
			}
		}
		
		protected function listenMyChipEvents(): void {
			_myChip.addEventListener(df_chip.MOVECHIP, doUpdateChip);
			_myChip.addEventListener(df_spaceObject.DISPOSE, doChipDispose);
		}
		
		protected function removeMyChipEvents(): void {
			_myChip.removeEventListener(df_chip.MOVECHIP, doUpdateChip);
			_myChip.removeEventListener(df_spaceObject.DISPOSE, doChipDispose);
			ctrl.removeEventListener(df_element.SETMODEL, doSetModel);
		}
		
		protected function doChipDispose(e: Event): void {
			removeMyChipEvents();
			_myChip = null;
			ctrl.Space.callWaitEvent(df_space.SETMYCHIP, resetMyChip);
		}
		
		protected function getNearest(): Number {
			Bounds.getObjectContainerBounds(ctrl.getModel());
			var minDistance: Number = Math.max(Bounds.width, Math.max(Bounds.height, Bounds.depth)) * 
				(options.nearestScale?options.nearestScale:1);
			return minDistance + _myChip.attackableRadius;
		}
		
		protected function doUpdateChip(e: Event): void {
			if (ctrl.getModel().visible) { 
				nearState = checkNear();
			}
		}
		
		protected function get nearState(): Boolean {
			return _nearState;
		}
		
		protected function set nearState(a_nearState: Boolean): void {
			if (_nearState != a_nearState) setNearState(a_nearState);
		}
		
		protected function setNearState(a_nearState: Boolean): void {
			_nearState = a_nearState;
			if (_nearState) doNearest();
		}
		
		protected function checkNear(): Boolean {
			var p: Vector3D = ctrl.getModel().position;
			var dist: Number = p.subtract(_myChip.body.currentState.position).length;
			return dist <= _nearest;
		}
		
		protected function doNearest(): void {
			playSound('nearest', ctrl.getModel());
		}
		
		override public function dispose():void {
			if (_myChip) removeMyChipEvents();
			super.dispose();
		}
	}
}